using B.S.BaseData.ErrorCode;
using B.S.BaseData.ErrorCode.ErrorCodes;
using B.S.BaseData.WriteAPI.Utils;
using B.S.DataBase.EFCoreServiers.Dtos.Roles;
using B.S.DataBase.EFCoreServiers.IServices.Roles;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace B.S.BaseData.WriteAPI.Controllers
{
    /// <summary>
    /// 角色管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    [ApiExplorerSettings(GroupName = "Roles")]
    public class RoleController : ControllerBase
    {
        private readonly IRoleServices services;
        private readonly ILogger<RoleController> _logger;

        public RoleController(IRoleServices services, ILogger<RoleController> logger)
        {
            this.services = services;
            _logger = logger;
        }

        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="roleDto">角色信息</param>
        /// <returns>添加结果</returns>
        [HttpPost]
        [LogOperation("角色管理", "新增", "添加新角色")]
        public async Task<ApiResult> AddRole([FromBody] AddRoleDto roleDto)
        {
            try
            {
                var res = await services.AddRoleAsync(roleDto);
                if (res != null)
                {
                    _logger.LogInformation("开始添加角色: {RoleName}", roleDto.RoleName);
                }
                return res;
            }
            catch (Exception ex)
            {
                return ApiResult.Fail("添加角色失败：" + ex.Message, StatusCodeEnum.Exception);
            }
        }
        /// <summary>
        /// 编辑角色
        /// </summary>
        /// <param name="roleDto"></param>
        /// <returns></returns>
        [HttpPut]
        [LogOperation("角色管理", "修改", "更新角色信息")]
        public async Task<ApiResult> UpdateRoleAsync([FromBody] UpdateRoleDto roleDto)
        {
            try
            {
                var res = await services.UpdateRoleAsync(roleDto);
                if (res != null)
                {
                    _logger.LogInformation("开始编辑角色: ", roleDto.RoleName);
                }
                return res;
            }
            catch (Exception ex)
            {
                return ApiResult.Fail("编辑角色失败：" + ex.Message, StatusCodeEnum.Exception);
            }
        }
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="roleDto"></param>
        /// <returns></returns>
        [HttpDelete]
        [LogOperation("角色管理", "删除", "删除角色")]
        public async Task<ApiResult> DeleteRoleAsync(int roleId)
        {
            try
            {
                var res = await services.DeleteRoleAsync(roleId);
                if (res != null)
                {
                    _logger.LogInformation("开始删除角色: ", roleId);
                }
                return res;
            }
            catch (Exception ex)
            {
                return ApiResult.Fail("删除 角色失败：" + ex.Message, StatusCodeEnum.Exception);
            }
        }
        /// <summary>
        /// 修改角色状态
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpPut]
        [LogOperation("角色管理", "状态修改", "修改角色启用状态")]
        public async Task<ApiResult> UpdateStateRoleAsync(int roleId)
        {
            try
            {
                var res = await services.UpdateStateRoleAsync(roleId);
                if (res != null)
                {
                    _logger.LogInformation("开始修改状态 角色: ", roleId);
                }
                return res;
            }
            catch (Exception ex)
            {
                return ApiResult.Fail("修改状态状态失败：" + ex.Message, StatusCodeEnum.Exception);
            }
        }



    }
}